package logic

import (
	"context"
	"errors"
	"novel-sys/model/mysql"

	"novel-sys/goods/goods"
	"novel-sys/goods/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BooksDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBooksDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BooksDelLogic {
	return &BooksDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BooksDelLogic) BooksDel(in *goods.BooksDelRequest) (*goods.BooksDelResponse, error) {
	// todo: add your logic here and delete this line
	data := mysql.Books{
		UserId:  in.UserId,
		NovelId: in.NovelId,
	}
	var book mysql.Books
	tx := mysql.DB.Begin()
	if err := book.BooksById(tx, (*goods.BooksAddRequest)(in)); err != nil {
		tx.Rollback()
		return nil, err
	}
	if book.ID == 0 {
		return nil, errors.New("不存在")
	}

	if err := data.BookDel(tx, in); err != nil {
		tx.Rollback()
		return nil, err
	}
	tx.Commit()
	return &goods.BooksDelResponse{}, nil
}
